package com.tool_stdy.thread;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by hanweijuan on 17/1/19.
 */
public class CyclicBarrierTest implements Runnable {

    public CyclicBarrierTest(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    private CyclicBarrier cyclicBarrier;


    public void run() {
        try {
            Thread.sleep(100*(new Random()).nextInt(5));
            System.out.println(Thread.currentThread().getName()+"--- wait");
            cyclicBarrier.await();
            System.out.println(Thread.currentThread().getName()+"--- Go");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(2);

        Executor executor = Executors.newFixedThreadPool(3);

        for (int i=0;i<2;i++){
            executor.execute(new CyclicBarrierTest(cyclicBarrier));
        }
    }
}
